﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FifteenPuzzleGUI
{
    public partial class Form1 : Form
    {
        private Board puzzle = new Board();
        private Tile zero = new Tile();
        private Tile neighbour = new Tile();

        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(210, 225);
            puzzle.randomMoves();
            Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            puzzle.Draw(e.Graphics);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            zero = puzzle.findZero();
            switch (e.KeyCode)
            {
                case Keys.Left:
                    if (zero.colPos > 0)
                    {
                        neighbour = puzzle.getNeighbour("left", zero);
                        puzzle.swap(zero, neighbour);
                    }
                    this.Invalidate();
                    break;

                case Keys.Right:
                    if (zero.colPos < 3)
                    {
                        neighbour = puzzle.getNeighbour("right", zero);
                        puzzle.swap(zero, neighbour);
                    }
                    this.Invalidate();
                    break;

                case Keys.Up:
                    if (zero.rowPos > 0)
                    {
                        neighbour = puzzle.getNeighbour("up", zero);
                        puzzle.swap(zero, neighbour);
                    }
                    this.Invalidate();
                    break;
                case Keys.Down:
                    if (zero.rowPos < 3)
                    {
                        neighbour = puzzle.getNeighbour("down", zero);
                        puzzle.swap(zero, neighbour);
                    }
                    this.Invalidate();
                    break;

                default:
                    break;
            }
            bool gameWon = false;
            gameWon = puzzle.checkForWin();

            if (gameWon == true)
            {
                MessageBox.Show("You Win", "Congratulations");
            }

        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    if (puzzle.board[row, col].contains(e.X, e.Y))
                    {
                        zero = puzzle.findZero();

                        if ( ( ( (puzzle.board[row, col].rowPos == zero.rowPos - 1) || (puzzle.board[row, col].rowPos == zero.rowPos + 1) ) && (puzzle.board[row, col].colPos == zero.colPos) ) )
                        {
                            puzzle.swap(puzzle.board[row, col], zero);
                            Invalidate();
                        }
                        else if (((puzzle.board[row, col].colPos == zero.colPos - 1) || (puzzle.board[row, col].colPos == zero.colPos + 1)) && (puzzle.board[row, col].rowPos == zero.rowPos))
                        {
                            puzzle.swap(puzzle.board[row, col], zero);
                            Invalidate();
                        }
                    }

                }
            }
        }



    }
}
